<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组唯一性检查</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f7fc;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }

        h1 {
            font-size: 24px;
            color: #333;
            margin-bottom: 20px;
        }

        input {
            width: 100%;
            padding: 10px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 5px;
            margin-bottom: 20px;
        }

        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            font-size: 18px;
        }

        .unique {
            color: green;
        }

        .duplicate {
            color: red;
        }

        .error {
            color: #ff5722;
            font-size: 14px;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>检查数组唯一性</h1>
        <input type="text" id="inputArray" placeholder="输入数组（例如：[1,2,3]）" />
        <button onclick="checkUniqueness()">检查</button>
        <div id="result" class="result"></div>
        <div id="errorMessage" class="error"></div>
    </div>

    <script>
        // 判断数组是否包含重复项
        const allUnique = arr => arr.length === new Set(arr).size;

        // 检查用户输入的数组
        function checkUniqueness() {
            const input = document.getElementById('inputArray').value;
            const resultElement = document.getElementById('result');
            const errorMessageElement = document.getElementById('errorMessage');

            // 清空之前的错误信息和结果
            errorMessageElement.textContent = '';
            resultElement.textContent = '';

            try {
                // 将输入的字符串转换为数组
                const parsedArray = JSON.parse(input);

                // 判断输入是否为数组
                if (!Array.isArray(parsedArray)) {
                    throw new Error('请输入有效的数组格式，例如：[1, 2, 3]');
                }

                // 检查数组是否唯一
                if (allUnique(parsedArray)) {
                    resultElement.textContent = '数组中的所有元素都是唯一的！';
                    resultElement.className = 'result unique';
                } else {
                    resultElement.textContent = '数组中存在重复元素！';
                    resultElement.className = 'result duplicate';
                }

            } catch (error) {
                errorMessageElement.textContent = '错误：' + error.message;
            }
        }
    </script>

</body>

</html>